package org.sky.sys.service;

import org.apache.logging.log4j.Logger;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysOrgan;
import org.sky.sys.mapper.SysCommonMapper;
import org.sky.sys.mapper.SysOrganMapper;
import org.sky.sys.util.BspUtils;
import org.sky.sys.util.TreeStru;
import org.sky.util.LogUtils;
import org.sky.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author weifx
 * @Title: SysOrganService
 * @ProjectName springboot-platform
 * @Description: TODO{组织机构业务类}
 * @date 2019/1/23 18:18
 * @Version 1.0
 */
@Service
public class SysOrganService {
    private Logger log = LogUtils.getLogger(SysOrganService.class);

    @Autowired
    private SysOrganMapper sysOrganMapper;
    @Autowired
    private SysCommonMapper sysCommonMapper;
    /**
     * 根据父节点编号查询组织机构树
     * @param parCode
     * @return
     */
    public List<TreeStru> getSysOrganTree(String parCode){
        List<TreeStru> list = new ArrayList();
        Example soe = new Example(SysOrgan.class);
        soe.createCriteria().andEqualTo("parCode",parCode);
        soe.setOrderByClause("seq asc");
        List<SysOrgan> solist = sysOrganMapper.selectFullNameByExample(soe);
        for(SysOrgan so:solist){
            TreeStru ts = new TreeStru();
            ts.setId(so.getId().toString());
            ts.setCode(so.getCode());
            ts.setText(so.getName()+"["+so.getCode()+"]");
            ts.setSeq(null==so.getSeq()?0:so.getSeq());
            ts.setIconCls(so.getIcon());
            if(so.getChildCount()>0){
                ts.setState("closed");
            }else{
                ts.setState("open");
            }
            ts.setData(so);
            list.add(ts);
        }
        return list;
    }

    /**
     * 根据code删除
     * @param code
     */
    @Transactional
    public void delSysOrganByCode(String code) throws ServiceException{
        try {
            Example example = new Example(SysOrgan.class);
            example.createCriteria().andEqualTo("parCode",code);
            int count = sysOrganMapper.selectCountByExample(example);
            if(count>0){
                throw new ServiceException("组织机构存在下级,不能删除");
            }
            SysOrgan sysOrgan = new SysOrgan();
            sysOrgan.setCode(code);
            sysOrganMapper.delete(sysOrgan);
        }catch (Exception e){
            e.printStackTrace();
            log.error("组织机构删除失败:{}",e.getMessage());
            throw new ServiceException("删除失败:"+e.getMessage(),e);
        }
    }

    /**
     * 根据主键查询组织机构
     * @param id
     * @return
     */
    public SysOrgan getSysOrganById(Long id){
        Example example = new Example(SysOrgan.class);
        example.createCriteria().andEqualTo("id",id);
        return sysOrganMapper.selectOneByExample(example);
    }

    /**
     * 保存{新增/修改}组织机构
     * @param sysOrgan
     */
    public void saveAddEditSysOrgan(SysOrgan sysOrgan){
        try{
            Timestamp ts = sysCommonMapper.queryTimestamp();
            if(null == sysOrgan.getId()){
                //新增
                sysOrgan.setId(SnowflakeIdWorker.getInstance().nextId());
                sysOrgan.setCreater(BspUtils.getLoginUser().getCode());
                sysOrgan.setCreateTime(ts);
                sysOrgan.setUpdater(BspUtils.getLoginUser().getCode());
                sysOrgan.setUpdateTime(ts);
                sysOrganMapper.insertSelective(sysOrgan);
            }else{
                //修改
                sysOrgan.setUpdater(BspUtils.getLoginUser().getCode());
                sysOrgan.setUpdateTime(ts);
                Example example = new Example(SysOrgan.class);
                example.createCriteria().andEqualTo("id",sysOrgan.getId());
                sysOrganMapper.updateByExampleSelective(sysOrgan,example);
            }
        }catch(Exception e){
            log.error("组织机构保存失败:{}",e.getMessage());
            throw new ServiceException("组织机构保存失败:"+e.getMessage(),e);
        }
    }

    /**
     * 查询所有机构信息
     * @return
     */
    public List<SysOrgan> selectAll(){
        List<SysOrgan> list = sysOrganMapper.selectAll();
        return list;
    }
    /**
     * 批量插入
     * @param sysOrganList
     */
    @Transactional
    public void batchInsert(List<SysOrgan> sysOrganList){
        Timestamp ts = sysCommonMapper.queryTimestamp();
        for(SysOrgan sysOrgan:sysOrganList) {
            sysOrgan.setId(SnowflakeIdWorker.getInstance().nextId());
            //sysOrgan.setCreater(BspUtils.getLoginUser().getCode());
            sysOrgan.setCreateTime(ts);
            //sysOrgan.setUpdater(BspUtils.getLoginUser().getCode());
            sysOrgan.setUpdateTime(ts);
            sysOrganMapper.insertSelective(sysOrgan);
        }
    }
    /**
     * 批量更新
     * @param sysOrganList
     */
    @Transactional
    public void batchUpdate(List<SysOrgan> sysOrganList){
        Timestamp ts = sysCommonMapper.queryTimestamp();
        Example example = new Example(SysOrgan.class);

        for(SysOrgan sysOrgan:sysOrganList) {
            //sysOrgan.setUpdater(BspUtils.getLoginUser().getCode());
            sysOrgan.setUpdateTime(ts);
            example.clear();
            example.createCriteria().andEqualTo("code",sysOrgan.getCode());
            sysOrganMapper.updateByExampleSelective(sysOrgan,example);
        }
    }
}
